// pages/market/shoppingCar/shoppingCar.js
const app = getApp()
const request = require('../../../utils/request');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    checked: false,
    checked_all: false,
    icon: {
      normal: '/img/click_out.png',
      active: '/img/click_in.png',
      normal_all: '/img/click_out.png',
      active_all: '/img/click_blue_in.png'
    },
    cartList: [],
    allPrice: 0,
    cart_id: "",
    oid: 0
  },
  // 单选
  changeSelect(e) {
    var index = e.currentTarget.dataset.id;
    this.data.cartList[index].select = !this.data.cartList[index].select;
    this.setData({
      cartList: this.data.cartList,
    });
    this.getPrice();
  },
  // 全选
  allCheck: function() {
    var allPrice = 0;
    this.data.checked_all = !this.data.checked_all;
    if (this.data.checked_all) {
      this.data.cartList.forEach(item => {
        item.select = true
        allPrice = allPrice + parseInt(item.price);
      })
    } else {
      this.data.cartList.forEach(item => {
        item.select = false
        allPrice = 0
      })
    }
    this.setData({
      checked_all: this.data.checked_all,
      cartList: this.data.cartList,
      allPrice: allPrice
    });
  },
  getPrice() {
    var allPrice = 0;
    var cart_id = "";
    for (var i = 0; i < this.data.cartList.length; i++) {
      if (this.data.cartList[i].select == true) {
        cart_id += this.data.cartList[i].id + ",";
        allPrice += parseInt(this.data.cartList[i].price);
        // self.trueArr.push(self.cartList[i].status);
      }
    }
    this.setData({
      allPrice: allPrice,
      cart_id: cart_id
    })
  },
  // 提交订单
  goPay: function() {
    wx.showLoading()
    const data = {
      token: wx.getStorageSync("token"),
      cart_id: this.data.cart_id,
    }
    request.get('cart_jiesuan', data)
      .then(res => {
        wx.hideLoading()
        wx.showToast({
          icon: 'none',
          title: res.msg
        })
        setTimeout(() => {
          wx.navigateTo({
            url: '../shoppingCar/payment/payment?oid=' + res.oid,
          })
        }, 1000)
      })
      .catch(error => {
        wx.showToast({
          icon: 'none',
          title: error.msg && error.msg
        })
      })
      .finally(_ => {
        
      })
  },
  getCartList() {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })

    wx.request({
      url: app.d.severUrl + 'shopCart',
      method: 'GET',
      data: {
        token: wx.getStorageSync("token"),
      },
      success: res => {
        wx.hideLoading();
        if (res.data.code == 0) {
          
          var content = res.data.data;
          content.forEach(item => {
            item.select = false
          })

          that.setData({
            cartList: content
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.data.msg
          })
        }
      },
      fail: error => {
        wx.showToast({
          icon: 'none',
          title: "网络错误，请稍后重试"
        })
      }
    })
  },
  // 删除
  delete() {
    wx.showLoading()
    const data = {
      token: wx.getStorageSync("token"),
      collect_id: this.data.cart_id,
    }
    request.get('del_cart', data)
      .then(res => {
        wx.hideLoading()
        wx.showToast({
          icon: 'none',
          title: res.msg
        })
        setTimeout(()=>{
          this.getCartList();
        },800)
       
      })
      .catch(error => {
        wx.showToast({
          icon: 'none',
          title: error.msg && error.msg
        })
      })
      .finally(_ => {
        
      })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getCartList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})